export default class compass {
  constructor() {
    this.enabled = false
    //this.interval = "game" //ui game normal
    this.data = { direction : 0, accuracy : "unknown",offset : 0}
  }
  startSuccess() { }
  startFail() { }
  startComplete() { }

  stopSuccess() { }
  stoptFail() { }
  stopComplete() { }


  change(data) {
    if (typeof (data.accuracy) == "number"){
      this.offset = data.accuracy 
    } else {
      this.accuracy = data.accuracy
    }
    this.data.delta = data.direction - this.data.direction
    this.data.direction = data.direction
    this.data.changed = this.data.delta == 0
    if (this.data.changed) {
      this.onChanged.bind(this)()
    }
  }
  start() {
    wx.startCompass({
      //interval: this.interval,
      success: this.startSuccess.bind(this),
      fail: this.startFail.bind(this),
      complete: this.startComplete.bind(this)
    })
    wx.onCompassChange(this.change.bind(this))
    this.enabled = true
  }
  stop() {
    wx.stopCompass({
      success: this.stopSuccess.bind(this),
      fail: this.stopFail.bind(this),
      complete: this.stopComplete.bind(this)
    })
    this.enabled = false
  }
  onChanged() { }

  getLocation(type,height,cond){
    wx.getLocation({
      type : type,
      altitude : height,
      success : cond.success,
      fail: cond.fail,
      complete:cond.complete,
    })
  }
}